package org.springblade.app.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springblade.core.http.util.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class UinoUtil {
	@Value("${built-in.twin-app:http://192.168.1.212:1662/thing}")
	private String twinUrl = "http://192.168.1.212:1662/thing";
	@Value("${twin.uino.username:admin}")
	private String username = "admin";
	@Value("${twin.uino.password:GbEGJ7LOiE3PJrdaPBO6fg==}")
	private String password = "GbEGJ7LOiE3PJrdaPBO6fg==";
	@Value("${twin.uino.domainId:1224141613300088}")
	private Long domainId = 1224141613300088L;

	/**
	 * 调用登录接口获取token
	 * @param username
	 * @param password
	 * @param domainId
	 * @return
	 */
	public  String getToken(String username, String password, Long domainId){
		String token = "";
		String loginUrl = twinUrl  + "/dcv/user/oauth/login";
		Map<String, Object> parameters = new HashMap<>();
		parameters.put("loginCode", username);
		parameters.put("password", password);
		if (domainId != 0){
			parameters.put("domainId", domainId);
		}
		String result = HttpUtil.postJson(loginUrl, JSON.toJSONString(parameters));
		JSONObject jsonObject = JSON.parseObject(result);
		if ((Boolean)jsonObject.get("success")){
			JSONObject data = (JSONObject) jsonObject.get("data");
			token = (String) data.get("token");
		}
		return token;
	}

	/**
	 * 添加域,返回domainID
	 * @param username
	 * @param password
	 * @param domainName
	 * @param token
	 * @return domainId
	 */
	public Long addDomain(String username, String password, String domainName, String token){
		Long domainId = 0L;
		String domainUrl = twinUrl + "/sys/tenantDomain/saveOrUpdate";
		Map<String, Object> parameters = new HashMap<>();
		parameters.put("loginCode", username);
		parameters.put("password", password);
		parameters.put("name", domainName);
		Map<String, String> header = new HashMap<>();
		header.put("tk", token);
		String result = HttpUtil.postJson(domainUrl, header, JSON.toJSONString(parameters));
		// TODO 执行时间过长, 密码加密方式未确定
		JSONObject jsonObject = JSON.parseObject(result);
		if ((Boolean)jsonObject.get("success")){
			domainId = (Long) jsonObject.get("data");
		} else {
			System.out.println(jsonObject.get("message"));
		}
		return domainId;
	}

	/**
	 * 添加用户
	 * @param account
	 * @param userName
	 * @param token
	 * @return
	 */
	public Long addUser(String account, String userName, String token){
		Long userId = 0L;
		String domainUrl = twinUrl + "/permission/user/saveOrUpdate";
		Map<String, Object> parameters = new HashMap<>();
		parameters.put("loginCode", account);
		parameters.put("userName", userName);
		Map<String, String> header = new HashMap<>();
		header.put("tk", token);
		String result = HttpUtil.postJson(domainUrl, header, JSON.toJSONString(parameters));
		JSONObject jsonObject = JSON.parseObject(result);
		if ((Boolean)jsonObject.get("success")){
			userId = (Long) jsonObject.get("data");
		} else {
			System.out.println(jsonObject.get("message"));
		}
		return userId;
	}

	public boolean removeUser(Long userId, String token){
		String domainUrl = twinUrl + "/permission/user/removeUserById";
		Map<String, String> header = new HashMap<>();
		header.put("tk", token);
		String result = HttpUtil.postJson(domainUrl, header, JSON.toJSONString(userId));
		JSONObject jsonObject = JSON.parseObject(result);
		if ((Boolean)jsonObject.get("success")){
			return true;
		} else {
			System.out.println(jsonObject.get("message"));
			return false;
		}
	}

	public Long addUserDefault(String account, String userName){
		String token = getToken(username, password, domainId);
		return addUser(account, userName, token);
	}

	public boolean removeUserDefault(Long userId){
		String token = getToken(username, password, domainId);
		return removeUser(userId, token);
	}

}
